<template>
	<div class="city-list" ref="wrapper">
		<div>
			<div class="list-area">
				<div class="list-title border-topbottom">当前位置</div>
				<div class="list-content">
					<div class="button-wrapper">
						<div class="button">{{this.$store.state.city}}</div>
					</div>
				</div>
			</div>
			<div class="list-area">
				<div class="list-title border-topbottom">热门城市</div>
				<div class="list-content">
					<div 
						class="button-wrapper" 
						v-for="item of hotCity" 
						:key="item.id"
						@click="cityClickHandle(item.name)"
					>
						<div class="button">{{item.name}}</div>
					</div>
				</div>
			</div>
			<div 
				class="list-area" 
				v-for="(alphabet,key) of cities" 
				:key="key"
				:ref="key"
			>
				<div class="list-title border-topbottom">{{key}}</div>
				<div class="list-content">
					<div 
						class="city-item" 
						v-for="cityItem of alphabet" 
						:key="cityItem.id"
						@click="cityClickHandle(cityItem.name)"
					>
						{{cityItem.name}}
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import BScroll from 'better-scroll'
	export default {
		name:'cityList',
		props: {
			cities:Object,
			hotCity:Array,
			letter:String
		},
		mounted() {
			/*使用better-scroll*/
			this.scroll=new BScroll(this.$refs.wrapper,{
				click:true
			});
		},
		methods: {
			cityClickHandle(city) {
				this.$store.commit('changeCityState',city)
				/*跳转到首页*/
				this.$router.push('/');
			}
		},
		watch: {
			/**
			 * 监听letter的改变
			 * 如果letter存在
			 * 字母列表被点击，并且被点击的字母被传递过来了
			*/
			letter() {
				if(this.letter) {
					/**
					 * this.$refs[this.letter]得到的是一个数组
					 * 并且数组长度为1
					 */
					const element=this.$refs[this.letter][0];
					/*better-scroll自动滚到某个元素*/
					this.scroll.scrollToElement(element);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import 'styles/mixins';
	.border-topbottom {
		&:before {
			background-color: #ccc;
		}
		&:after {
			background-color: #ccc;
		}
	}
	.border-bottom {
		&:before {
			background-color: #ccc;
		}	}
	.city-list {
		overflow: hidden;
		position: absolute;
		top: 1.62rem;
		left: 0;
		bottom: 0;
		right: 0;
		.list-title {
			height: .65rem;
			line-height: .65rem;
			background-color: #f5f5f5;
			padding-left: .2rem;
			color:#212121;
			font-size: .26rem;
		}
		.list-content {
			padding: .1rem .6rem .1rem .1rem;
			overflow: hidden;
			.button-wrapper {
				float: left;
				width: 33.33%;
				.button {
					margin: .1rem;
					padding: .1rem 0;
					text-align: center;
					border:.02rem solid #ccc;
					border-radius: .06rem
				}
			}
			.city-item {
				line-height: .76rem;
        		padding-left: .2rem;
        		@include textEllipsis;
			}
		}
	}
</style>